import Vue from "vue"
import Vuex from 'vuex';
import Spcar from "@/vuex/Spcar.js"
import Mt from "@/meituan/Mt.js"
import Lt from "@/vuex/lmt.js"
import VuexPersist from 'vuex-persist'
//创建vuexLocal并声明内部长久储存方式
const vuexLocal = new VuexPersist({
    storage: window.localStorage //可选，sessionStorage/indexDB
});
Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        add:
            [{ name: "周杰伦", age: 80 },
            { name: "马云", age: 20 },
            { name: "李四", age: 30 },
            { name: "张三", age: 12 }],
        arr: [
            {
                name: "【5本26.8元】经典儿童文学彩图青少版八十天环游地球中学生语文教学大纲",
                price: "￥12.6",
                check: false,
                dd: 1,
                str: 12.6,
                c: true,
            },
            {
                name: "【2000张贴纸】贴纸书 3-6岁 贴画儿童 贴画书全套12册 贴画 贴纸儿童 汽",
                price: "￥24.80",
                check: false,
                dd: 1,
                str: 24.80,
                c: true,
            },
            {
                name: "唐诗三百首+成语故事全2册 一年级课外书 精装注音儿童版 小学生二三年级课外阅读书籍",
                price: "￥29.80",
                check: false,
                dd: 1,
                str: 29.80,
                c: true,
            },
        ],
        s: 0,
        p: 0,
        che: "",
        h: "",
        p: [],
        n: 0
    },
    // mutations 用来声明修改数据的方法
    mutations: {
        //方法里面不用写$store
        add(sta, i) {
            i.dd++;
            this.pr = i.price.split("￥")[1];
            i.str = i.dd * this.pr;
            this.soop = i.dd
            this.state.s++

        },
        odd(sta, i) {
            if (i.dd > 1) {
                this.state.s = i.dd
                i.dd--;
                this.pr = i.price.split("￥")[1];
                i.str = i.dd * this.pr;
                this.state.s--
            }
        },
        del(sta, i) {
            i.c = false
        },
        check() {
            this.state.arr.forEach(item => {
                item.check = this.state.che
            })
        },
        ck() {
            this.state.che = this.state.arr.every((item) => item.check);
        },
        dl(state, i) {
            state.h = i
            console.log(i);
        },
        setN() {
            this.state.n++
        }
    },
    //相当于state的计算属性,这里不用写this
    getters: {
        fn(state) {
            var sum = 0
            state.arr.forEach(item => {
                if (item.check == true) {
                    sum = item.str * item.dd
                }
            })
            return sum
        },
        od(state) {
            var num = 0
            state.arr.forEach(item => {
                if (item.check == true) {
                    num++
                }
            })
            return num
        }
    },
    //模块 把具体的功能模块的vuex单独写成一个文件
    modules: {
        Spcar,  //对应模块的js文件的声明
        Mt,
        Lt
    },
    plugins: [vuexLocal.plugin],
    actions: {
        Zactions(state) {
        state.commit("setN")
        }
    }
})



